<?php

include ('config.php');

class Kegiatan {

	private $connect;
	public $data;

	public function __construct() {
		
	}

	public function safe_query ($query = "") {
		if(empty($query)){
			return false;
		}
		$result = mysql_query($query);
		if ($result) {
		  $log = fopen("query.log", "ab");
		  $recDate = date("d-m-Y H:i");
		  $loginfo = "[$recDate] query=".$query."\n";
		  fwrite($log, $loginfo);
		  fclose($log);
		  return $result;
		} else {
		  $log = fopen("failquery.log", "ab");
		  $recDate = date("d-m-Y H:i");
		  $loginfo = "[$recDate] query failed: errorno=".mysql_errno().";error=".mysql_error().";\nquery=".$query."\n";
		  fwrite($log, $loginfo);
		  fclose($log);
		  return FALSE;
		}
	}
	
	public function lihat ($table,$table2,$table3,$order,$id) {
		if ($id == "") {
			$query = "SELECT ".$table.".*, 
					 ".$table2.".nama_kabkota as `kabupaten`, 
					 ".$table3.".nama_sektoral as `sektoral` 
					 FROM `".$table."`
					 JOIN `".$table2."`  ON ".$table2.".idkabkota  = ".$table.".idkabkota
					 JOIN `".$table3."`  ON ".$table3.".idsektoral  = ".$table.".idsektoral
					 ORDER BY `".$order."`";
		} else {
			$query = "SELECT ".$table.".*, 
					 ".$table2.".nama_kabkota as `kabupaten`, 
					 ".$table3.".nama_sektoral as `sektoral`,
					 ".$table3.".icon as `icon`  
					 FROM `".$table."`
					 JOIN `".$table2."` ON ".$table2.".idkabkota   = ".$table.".idkabkota
					 JOIN `".$table3."` ON ".$table3.".idsektoral  = ".$table.".idsektoral
					 WHERE `idkegiatan` = '".$id."'";
		}

		$result = $this->safe_query($query);
		if(($result) && (mysql_num_rows($result) > 0)) {
			$data = array();
			while($row = mysql_fetch_assoc($result))
			$data[] = $row;
			return $data;
		} else {
			return FALSE;
		}
	}
	
	public function lihat_5kegiatan ($table,$table2,$table3,$id) {
		$query = "SELECT ".$table.".*, ".$table2.".nama_kabkota as `kabupaten`, ".$table2.".idkabkota, 
				 ".$table3.".nama_sektoral as `sektoral`,  ".$table3.".idsektoral FROM `".$table."` 
				 JOIN `".$table2."`  ON  ".$table2.".idkabkota  =  ".$table.".idkabkota
				 JOIN `".$table3."`  ON  ".$table3.".idsektoral =  ".$table.".idsektoral 
				 ORDER BY `idkegiatan` DESC";
		$result = $this->safe_query($query);
		if(($result) && (mysql_num_rows($result) > 0)) {
			$data = array();
			while($row = mysql_fetch_assoc($result))
			$data[] = $row;
			return $data;
		} else {
			return FALSE;
		}
	}
	
	public function lihat_kegiatan_dari_kabupaten ($table,$table2,$table3,$id) {
		$query = "SELECT ".$table.".*, ".$table2.".nama_kabkota as `kabupaten`, ".$table2.".idkabkota, 
				 ".$table3.".nama_sektoral as `sektoral`,  ".$table3.".idsektoral FROM `".$table."` 
				 JOIN `".$table2."`  ON  ".$table2.".idkabkota  =  ".$table.".idkabkota
				 JOIN `".$table3."`  ON  ".$table3.".idsektoral =  ".$table.".idsektoral 
				 WHERE  ".$table.".idkabkota = '".$id."'
				 ORDER BY `idkegiatan`";
		$result = $this->safe_query($query);
		if(($result) && (mysql_num_rows($result) > 0)) {
			$data = array();
			while($row = mysql_fetch_assoc($result))
			$data[] = $row;
			return $data;
		} else {
			return FALSE;
		}
	}
	
	public function lihat_kegiatan_dari_sektoral ($table,$table2,$table3,$id) {
		$query = "SELECT ".$table.".*, ".$table2.".nama_kabkota as `kabupaten`, ".$table2.".idkabkota, 
				 ".$table3.".nama_sektoral as `sektoral`,  ".$table3.".idsektoral FROM `".$table."` 
				 JOIN `".$table2."`  ON  ".$table2.".idkabkota  = ".$table.".idkabkota
				 JOIN `".$table3."`  ON  ".$table3.".idsektoral = ".$table.".idsektoral 
				 WHERE ".$table.".idsektoral = '".$id."'
				 ORDER BY `idkegiatan`";
		$result = $this->safe_query($query);
		if(($result) && (mysql_num_rows($result) > 0)) {
			$data = array();
			while($row = mysql_fetch_assoc($result))
			$data[] = $row;
			return $data;
		} else {
			return FALSE;
		}
	}
	
	public function lihat_progres ($table,$id) {
		$query = "select max(progres) as `maks` from `".$table."` where `idkegiatan` = '".$id."'";
		$result = $this->safe_query($query);
		if(($result) && (mysql_num_rows($result) > 0)) {
			$data = array();
			while($row = mysql_fetch_assoc($result))
			$data[] = $row;
			return $data;
		} else {
			return FALSE;
		}
	}
	
	public function lihat_monitoring ($table,$id) {
		$query = "SELECT * from `".$table."` WHERE `idkegiatan` = '".$id."' ORDER BY `progres`";
		$result = $this->safe_query($query);
		if(($result) && (mysql_num_rows($result) > 0)) {
			$data = array();
			while($row = mysql_fetch_assoc($result))
			$data[] = $row;
			return $data;
		} else {
			return FALSE;
		}
	}
	
	public function tambah ($table,$data) {
		$fields = array();
		$values = array();
	    foreach($data as $key=>$value) {
			$fields[] ="`$key`";
			$values[] ="'$value'";
		}
	    $query = "INSERT INTO `".$table."` (".implode(',',$fields).") VALUES (".implode(',',$values).")";
		$result = $this->safe_query($query);
		if ($result && (mysql_affected_rows()>0))
			return TRUE;
		else
			return FALSE;
	}
	
	public function hapus ($table,$id) {
	    $query = "DELETE FROM `".$table."` WHERE `idkegiatan` = '".$id."'";	  
		$result = $this->safe_query($query);
		if ($result && (mysql_affected_rows()>0))
			return TRUE;
		else
			return FALSE;
	}
	
	public function ubah ($id, $table, $field) {
		$fields = array();
		foreach($field as $key=>$value) { 
			$fields[] = "`".$key."`"."="."'".$value."'";
		}							
	    $query = "UPDATE `".$table."` SET ".implode(',',$fields)." where idkegiatan="."'".$id."'";	  
		$result = $this->safe_query($query);
		if ($result && (mysql_affected_rows()>0))
			return TRUE;
		else
			return FALSE;
	}
}
